package managedbean.CRM.Discount; 
 
import entity.CRM.ProductPrice;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB; 
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage; 
import javax.faces.bean.ManagedBean; 
import javax.faces.context.FacesContext; 
import javax.faces.event.ActionEvent; 
import session.stateless.CRM.SaleInquiryBeanLocal;
import session.stateless.CRM.DiscountBeanLocal;
 
@ManagedBean(name="addDiscountManagerBean")
@RequestScoped
public class AddDiscountManagerBean implements Serializable { 
    @EJB 
    private DiscountBeanLocal discountBeanLocal; 
    @EJB
    private SaleInquiryBeanLocal saleInquiryBeanLocal;
     
    private long discountId;
    private String productType;
    private int lowerLimit;
    private int higherLimit;
    private double discountRate;
    
    private String statusMessage;
    
    public long getDiscountId() {
        return discountId;
    }
    public void setDiscountId(long discountId) {
        this.discountId = discountId;
    }

    public double getDiscountRate() {
        return discountRate;
    }
    public void setDiscountRate(double discountRate) {
        this.discountRate = discountRate;
    }

    public int getHigherLimit() {
        return higherLimit;
    }
    public void setHigherLimit(int higherLimit) {
        this.higherLimit = higherLimit;
    }

    public int getLowerLimit() {
        return lowerLimit;
    }
    public void setLowerLimit(int lowerLimit) {
        this.lowerLimit = lowerLimit;
    }

    public String getProductType() {
        return productType;
    }
    public void setProductType(String productType) {
        this.productType = productType;
    }
    
    public String getStatusMessage() { 
        return statusMessage; 
    }
    public void setStatusMessage(String statusMessage) { 
        this.statusMessage = statusMessage; 
    }

    
    public void saveNewDiscount(ActionEvent event) 
    { 
        try 
        { 
            if (discountBeanLocal.checkConflict(productType, lowerLimit, higherLimit)) {
                statusMessage = "Overlapping range limit. Please try again.";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Unsuccessful:" + statusMessage + "", ""));
            } else {
                discountId = discountBeanLocal.createDiscount(productType, lowerLimit, higherLimit, discountRate);
                statusMessage = "New discount Saved Successfully";
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Discount Result: " + statusMessage + " (New discount ID is " + discountId + ")", ""));
            }
        } 
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
    }
    
    public List<String> getProductTypes(){
        List<String> productTypes = new ArrayList<String>();
        for (Object o: saleInquiryBeanLocal.getProductPrice())
        {
            ProductPrice temp_ProductPrice = (ProductPrice)o;
            productTypes.add(temp_ProductPrice.getProductType());
        }
        return productTypes;        
    }
}
    

